package com.leng.service.impl;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.write.style.column.LongestMatchColumnWidthStyleStrategy;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.leng.VO.UserVO;
import com.leng.entity.User;
import com.leng.exception.BusinessException;
import com.leng.mapper.UserMapper;
import com.leng.response.ResultCode;
import com.leng.service.UserService;
import com.leng.utils.JWTUtils;
import com.leng.utils.MD5Utils;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author leng
 * @since 2022-08-21
 */
@Service
public class UserServiceImp extends ServiceImpl<UserMapper, User> implements UserService {

    @Override
    public Page<User> getUserByPage(Page<User> page, UserVO userVO) {
        List<User> userList = this.baseMapper.getUserList(page, userVO);
        //将刚刚动态sql查询的内容给page对象，然后去实现分页
        page.setRecords(userList);
        return page;
    }

    @Override
    public void exportExcel(HttpServletResponse response, List<User> users) throws IOException {
        //写入excel文件
        String exportFileName = "TbUserInfo.xlsx";
        //设置excel表格格式为utf-8
        //如果没有设置response相应头，导出文件的内容会乱码
        response.setCharacterEncoding("UTF-8");
        //设置导出文件的格式为excel
        response.setContentType("application/vnd.ms-excel");
        //以下载附件的方式进行 attachment:附件
        response.addHeader("Content-Disposition", "attachment;filename=" + exportFileName);
        EasyExcel.write(response.getOutputStream(), User.class)
         .sheet("用户信息")
         //自动列宽
         .registerWriteHandler(new LongestMatchColumnWidthStyleStrategy())
         //显示的数据
         .doWrite(users);
    }

    @Override
    public List<User> getUserListToExcel(UserVO userVO) {
        return this.baseMapper.getUserListToExcel(userVO);
    }

    @Override
    public String login(String username, String password) {
        String token=null;
        //根据用户名查询用户信息
        LambdaQueryWrapper<User> wrapper=new LambdaQueryWrapper();
        wrapper.eq(User::getUsername,username);
        User user = this.baseMapper.selectOne(wrapper);
        //用户不存在返回状态码
        if (user==null){
            throw new BusinessException(ResultCode.USER_ACCOUNT_NOT_EXIST.getCode(),
                    ResultCode.PARAM_IS_BLANK.getMessage());
        }
        //判断密码是否一致
        //获取到加密的密码
        String encryptPassword = user.getPassword();
        //获取到盐
        String salt=user.getSalt();
        //对获取的密码进行加密加盐
        String target = MD5Utils.md5Encryption(password, salt);
        System.out.println(user.getPassword()+"-------------"+password);
        System.out.println(target.equals(encryptPassword));
        //将加密的密码跟数据库的密码做比较
        if (!target.equals(encryptPassword)){
            throw new BusinessException(ResultCode.USER_CREDENTIALS_ERROR.getCode(),
                    ResultCode.USER_CREDENTIALS_ERROR.getMessage());
        }
        //如果成功，则生成token
        token= JWTUtils.generateToken(username,password);
        return token;
    }

    @Override
    public User getUserByUsername(String username) {
        return this.baseMapper.getUserByUsername(username);
    }
}